home *** CD-ROM | disk | FTP | other *** search
- Path: howland.reston.ans.net!psinntp!psinntp!psinntp!psinntp!usenet
- From: annoying@usa.pipeline.com(Zachary Hartnett)
- Newsgroups: comp.lang.c++
- Subject: Bit Fields in Structures
- Date: 24 Mar 1996 20:54:17 GMT
- Organization: Pipeline USA
- Message-ID: <4j4cpp$i8o@news1.h1.usa.pipeline.com>
- NNTP-Posting-Host: 38.8.60.5
- X-PipeUser: annoying
- X-PipeHub: usa.pipeline.com
- X-PipeGCOS: (Zachary Hartnett)
- X-Newsreader: Pipeline v3.5.0
-
- /* Here's one for all you C++ gurus out there! Why does the
- following code fragment fail to compile? I've included notes
- that seem to indicate it should...
-
- Thanks! (annoying@usa.pipeline.com)
- */
-
- /* NOTE:
- * Minimum sizes of fundamental types:
- * char: 8 bits
- * short: 16 bits
- * long: 32 bits
- * Ref: Bjarne Stroustrup, "The C++ Programming Language", 2d Ed.,
- * pg 50.
- */
-
- /* NOTE:
- * Types char, int of all sizes, and enumerations are collectively
- * called integral types.
- * Ref: Bjarne Stroustrup, "The C++ Programming Language", 2d Ed.,
- * pg 487.
- */
-
- /* NOTE:
- * For a more compact notation, int can be dropped from multiword
- * combinations without changing the meaning; thus long means long
- * int and unsigned means unsigned int.
- * Ref: Bjarne Stroustrup, "The C++ Programming Language", 2d Ed.,
- * pg 49-50.
- */
-
- struct DateStr
- {
- long month : 8; // WHY WON'T THIS COMPILE? (Borland C++ v4.52)
- long day : 8; // ERROR: Bit fields must have integral type
- long year : 16; // Only works with type int?! Why?
- };
-
- /* This is the structure I would LIKE to make:
- struct DateStr
- {
- unsigned long int month : 4; // 0-15 : Range is 1-12
- unsigned long int day : 5; // 0-31 : Range is 1-31
- unsigned long int year : 14; // 0-16383 : Range is 0000-9999
- unsigned long int epoch : 1; // 0-1 : Range is 0-1 (BC/AD)
- unsigned long int : 8;
- };
- */
-
- #include <iostream.h>
- #include <stdlib.h>
- void main(void)
- {
- DateStr date;
-
- cout << "sizeof (date) = " << sizeof (date) << '.';
- }
-
-